Node.js HTTP/2 Module

Node.js இல் HTTP/2 ப்ரோட்டோகால் செயல்படுத்தலைக் கற்றுக்கொள்ளுங்கள்

HTTP/2 என்றால் என்ன?

Node.js HTTP/2 தொகுதி HTTP/2 ப்ரோட்டோகாலின் ஒரு செயல்படுத்தலை வழங்குகிறது, இது மேம்படுத்தப்பட்ட செயல்திறன், சர்வர் புஷ் திறன்கள், ஹெடர் கம்ப்ரஷன் மற்றும் ஒரு ஒற்றை இணைப்பில் மல்டிப்ளக்சிங்கை வழங்குகிறது.

HTTP/2 பல முக்கிய அம்சங்களுடன் HTTP/1.1 ஐ மேம்படுத்துகிறது:

பைனரி ப்ரோட்டோகால்

HTTP/2 தரவு பரிமாற்றத்திற்கு உரை வடிவத்திற்குப் பதிலாக பைனரி ஃபார்மாட்டைப் பயன்படுத்துகிறது, இது பாகுபடுத்துவதற்கு மிகவும் திறமையானதாக்குகிறது

மல்டிப்ளக்சிங்

பல கோரிக்கைகள் மற்றும் பதில்கள் ஒரே நேரத்தில் ஒரு ஒற்றை இணைப்பில் அனுப்பப்படலாம்

ஹெடர் கம்ப்ரஷன்

HTTP/2 ஓவர்ஹெட்டைக் குறைக்க ஹெடர்களை சுருக்குகிறது

சர்வர் புஷ்

சர்வர்கள் வாடிக்கையாளர்கள் கோரிக்கை செய்வதற்கு முன்பே வளங்களைத் தீவிரமாக அனுப்ப முடியும்

ஸ்ட்ரீம் முன்னுரிமை

வளங்கள் வெவ்வேறு முன்னுரிமைகளுடன் வழங்கப்படலாம்

HTTP/2 தொகுப்பைப் பயன்படுத்துதல்

Node.js இல், HTTP/2 தொகுப்பைப் பயன்படுத்தி அணுகலாம்:

const http2 = require('http2');

HTTP/2 தொகுதி Node.js v10.0.0 இலிருந்து நிலையானது. பெரும்பாலான பிரௌசர்களில் HTTP/2 க்கு பாதுகாப்பான இணைப்பு (HTTPS) தேவைப்படுகிறது என்பதை கவனத்தில் கொள்ள வேண்டும், எனவே பெரும்பாலான எடுத்துக்காட்டுகள் TLS/SSL ஐப் பயன்படுத்தும்.

HTTP/2 சர்வரை உருவாக்குதல்

TLS ஐப் பயன்படுத்தி அடிப்படை HTTP/2 சர்வரை உருவாக்குவதற்கான எடுத்துக்காட்டு இங்கே:

const http2 = require('http2');
const fs = require('fs');
const path = require('path');

// Read the TLS certificate and key
const options = {
  key: fs.readFileSync(path.join(__dirname, 'server.key')),
  cert: fs.readFileSync(path.join(__dirname, 'server.crt'))
};

// Create an HTTP/2 server
const server = http2.createSecureServer(options);

// Handle stream events
server.on('stream', (stream, headers) => {
  // Get the path from headers
  const path = headers[':path'];
  
  // Send a response
  if (path === '/') {
    stream.respond({
      'content-type': 'text/html',
      ':status': 200
    });
    stream.end('

Hello from HTTP/2!

'); } else { stream.respond({ ':status': 404 }); stream.end('Not found'); } }); // Start the server const port = 8080; server.listen(port, () => { console.log(`HTTP/2 server running at https://localhost:${port}`); });

🔑 குறிப்பு:

இந்த எடுத்துக்காட்டு உங்களிடம் TLS சர்டிஃபிகேட் கோப்புகள் உள்ளன என்று கருதுகிறது. வளர்ச்சிக்காக, நீங்கள் OpenSSL ஐப் பயன்படுத்தி சுய-கையொப்பமிடப்பட்ட சான்றிதழ்களை உருவாக்கலாம். உற்பத்திக்கு, நம்பகமான சான்றிதழ் அதாரிட்டியைப் பயன்படுத்தவும்.

TLS இல்லாமல் HTTP/2 சர்வர்

நீங்கள் குறியாக்கம் இல்லாமல் நேரடி HTTP/2 இணைப்புகளுக்கு TLS இல்லாமல் ஒரு HTTP/2 சர்வரையும் உருவாக்கலாம்:

const http2 = require('http2');

// Create an HTTP/2 server without TLS
const server = http2.createServer();

server.on('stream', (stream, headers) => {
  stream.respond({
    'content-type': 'text/html',
    ':status': 200
  });
  stream.end('

Hello from HTTP/2 without TLS!

'); }); server.listen(8080);

⚠️ குறிப்பு:

பெரும்பாலான நவீன பிரௌசர்கள் TLS மூலம் HTTP/2 ஐ மட்டுமே ஆதரிக்கின்றன, எனவே பாதுகாப்பற்ற HTTP/2 சர்வர் பொதுவாக க்ளியர்டெக்ஸ்ட் HTTP/2 ஐ வெளிப்படையாக ஆதரிக்கும் அர்ப்பணிக்கப்பட்ட HTTP/2 கிளையன்ட்களுடன் மட்டுமே வேலை செய்யும்.

HTTP/2 கிளையன்ட்

HTTP/2 சர்வருடன் இணைக்க HTTP/2 கிளையன்டை உருவாக்குதல்:

const http2 = require('http2');

// Create a client
const client = http2.connect('https://localhost:8080', {
  // For self-signed certificates in development
  rejectUnauthorized: false
});

// Error handling
client.on('error', (err) => {
  console.error('Client error:', err);
});

// Create a request
const req = client.request({ ':path': '/' });

// Handle response data
req.on('response', (headers) => {
  console.log('Status:', headers[':status']);
  console.log('Headers:', headers);
});

req.on('data', (chunk) => {
  console.log('Received data:', chunk.toString());
});

req.on('end', () => {
  console.log('Request completed');
  client.close();
});

// Send the request
req.end();

HTTP/2 ஸ்ட்ரீம்கள்

HTTP/2 கிளையன்ட் மற்றும் சர்வருக்கு இடையேயான தகவல்தொடர்புக்கு ஸ்ட்ரீம்களைப் பயன்படுத்துகிறது. ஒவ்வொரு ஸ்ட்ரீமும் கிளையன்ட் மற்றும் சர்வருக்கு இடையே பரிமாறப்படும் சுயாதீன, இரு-திசை பிரேம்களின் வரிசையை பிரதிநிதித்துவப்படுத்துகிறது.

ஸ்ட்ரீம் நிகழ்வுகள்

முக்கியமான ஸ்ட்ரீம் நிகழ்வுகள்:

'headers'

ஹெடர்கள் பெறப்படும்போது உமிழப்படுகிறது

'data'

தரவின் ஒரு துண்டு பெறப்படும்போது உமிழப்படுகிறது

'end'

ஸ்ட்ரீம் முடிந்ததும் உமிழப்படுகிறது

'error'

ஒரு பிழை ஏற்படும்போது உமிழப்படுகிறது

எடுத்துக்காட்டு: ஸ்ட்ரீம் நிகழ்வுகளைக் கையாளுதல்

const http2 = require('http2');
const fs = require('fs');
const path = require('path');

// Create a server
const server = http2.createSecureServer({
  key: fs.readFileSync(path.join(__dirname, 'server.key')),
  cert: fs.readFileSync(path.join(__dirname, 'server.crt'))
});

server.on('stream', (stream, headers) => {
  // Handle stream events
  stream.on('error', (error) => {
    console.error('Stream error:', error);
  });
  
  stream.on('close', () => {
    console.log('Stream closed');
  });
  
  // Handle request
  stream.respond({
    'content-type': 'text/plain',
    ':status': 200
  });
  
  // Send data in multiple chunks
  stream.write('First chunk of data\n');
  
  setTimeout(() => {
    stream.write('Second chunk of data\n');
    stream.end('Final chunk of data');
  }, 1000);
});

server.listen(8080);

HTTP/2 சர்வர் புஷ்

சர்வர் புஷ் சர்வர் வாடிக்கையாளர்கள் வெளிப்படையாகக் கோருவதற்கு முன்பே வளங்களைத் தீவிரமாக அனுப்ப அனுமதிக்கிறது. இது ரவுண்ட்-ட்ரிப் தாமதங்களை நீக்குவதன் மூலம் செயல்திறனை மேம்படுத்தும்.

const http2 = require('http2');
const fs = require('fs');
const path = require('path');

const options = {
  key: fs.readFileSync(path.join(__dirname, 'server.key')),
  cert: fs.readFileSync(path.join(__dirname, 'server.crt'))
};

const server = http2.createSecureServer(options);

server.on('stream', (stream, headers) => {
  const requestPath = headers[':path'];
  
  if (requestPath === '/') {
    // Push CSS and JavaScript resources
    stream.pushStream({ ':path': '/style.css' }, (err, pushStream) => {
      if (err) {
        console.error('Error pushing stream:', err);
        return;
      }
      
      pushStream.respond({
        'content-type': 'text/css',
        ':status': 200
      });
      
      pushStream.end('body { color: blue; }');
    });
    
    stream.pushStream({ ':path': '/script.js' }, (err, pushStream) => {
      if (err) {
        console.error('Error pushing stream:', err);
        return;
      }
      
      pushStream.respond({
        'content-type': 'application/javascript',
        ':status': 200
      });
      
      pushStream.end('console.log("Hello from HTTP/2 server push!");');
    });
    
    // Send the main HTML document
    stream.respond({
      'content-type': 'text/html',
      ':status': 200
    });
    
    stream.end(`
      
      
      
        HTTP/2 Server Push Example
        
        
      
      
        

HTTP/2 Server Push Demo

CSS and JavaScript were pushed by the server!

`); } else { // Serve pushed resources if requested directly if (requestPath === '/style.css') { stream.respond({ 'content-type': 'text/css', ':status': 200 }); stream.end('body { color: blue; }'); } else if (requestPath === '/script.js') { stream.respond({ 'content-type': 'application/javascript', ':status': 200 }); stream.end('console.log("Hello from HTTP/2 server push!");'); } else { // Not found stream.respond({ ':status': 404 }); stream.end('Not found'); } } }); server.listen(8080);

HTTP/2 ஹெடர்கள்

HTTP/2 ஹெடர்களுக்கு வேறுபட்ட வடிவத்தைப் பயன்படுத்துகிறது. குறிப்பாக, அனைத்து ஹெடர்களும் லோவர்கேஸ் ஆகும், மேலும் கோரிக்கை சூடோ-ஹெடர்கள் கொலனுடன் தொடங்கும் (:).

const http2 = require('http2');

// HTTP/2 pseudo-headers
const {
  HTTP2_HEADER_METHOD,
  HTTP2_HEADER_PATH,
  HTTP2_HEADER_AUTHORITY,
  HTTP2_HEADER_SCHEME,
  HTTP2_HEADER_STATUS
} = http2.constants;

// Create a client
const client = http2.connect('https://localhost:8080', {
  rejectUnauthorized: false
});

// Send a request with custom headers
const req = client.request({
  [HTTP2_HEADER_METHOD]: 'GET',
  [HTTP2_HEADER_PATH]: '/',
  [HTTP2_HEADER_AUTHORITY]: 'localhost:8080',
  [HTTP2_HEADER_SCHEME]: 'https',
  'user-agent': 'node-http2/client',
  'custom-header': 'custom-value'
});

req.on('response', (headers) => {
  console.log('Response status:', headers[HTTP2_HEADER_STATUS]);
  console.log('Response headers:', headers);
});

req.on('data', (chunk) => {
  console.log('Received data:', chunk.toString());
});

req.on('end', () => {
  client.close();
});

req.end();

HTTP/2 செட்டிங்குகள்

HTTP/2 பல்வேறு ப்ரோட்டோகால் செட்டிங்குகளை கான்ஃபிகர் செய்ய அனுமதிக்கிறது:

const http2 = require('http2');
const fs = require('fs');
const path = require('path');

const options = {
  key: fs.readFileSync(path.join(__dirname, 'server.key')),
  cert: fs.readFileSync(path.join(__dirname, 'server.crt')),
  
  // HTTP/2 settings
  settings: {
    // Max concurrent streams per connection
    maxConcurrentStreams: 100,
    
    // Initial window size for flow control
    initialWindowSize: 1024 * 1024, // 1MB
    
    // Enable server push
    enablePush: true
  }
};

const server = http2.createSecureServer(options);

server.on('stream', (stream, headers) => {
  stream.respond({
    'content-type': 'text/html',
    ':status': 200
  });
  
  stream.end('

HTTP/2 Server with Custom Settings

'); }); server.listen(8080);

HTTP/1.1 உடன் இணக்கத்தன்மை

HTTP/2 சர்வர்கள் HTTP/1.1 கோரிக்கைகளையும் கையாள முடியும், இது ஒரு தடையற்ற அப்கிரேட் பாதையை வழங்குகிறது:

const http2 = require('http2');
const http = require('http');
const fs = require('fs');
const path = require('path');

// For HTTP/2 secure server
const options = {
  key: fs.readFileSync(path.join(__dirname, 'server.key')),
  cert: fs.readFileSync(path.join(__dirname, 'server.crt')),
  allowHTTP1: true // Allow HTTP/1.1 connections
};

const server = http2.createSecureServer(options);

// Handler function for both HTTP/1.1 and HTTP/2
const handler = (req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end(`Hello from ${req.httpVersion} server!`);
};

// HTTP/1.1 compatibility request handler
server.on('request', handler);

// HTTP/2 specific stream handler
server.on('stream', (stream, headers) => {
  stream.respond({
    'content-type': 'text/plain',
    ':status': 200
  });
  stream.end(`Hello from HTTP/2 stream API!`);
});

server.listen(8080, () => {
  console.log('Server running at https://localhost:8080/');
});

செயல்திறன் கருத்துகள்

HTTP/2 செயல்திறன் மேம்பாடுகளை வழங்கினாலும், உங்கள் பயன்பாட்டை உகந்ததாக்குவது முக்கியம்:

இணைப்பு மீண்டும் பயன்பாடு - HTTP/2 உடன், புதிய இணைப்புகளை உருவாக்குவதற்குப் பதிலாக பல கோரிக்கைகளுக்கு ஒரு ஒற்றை இணைப்பைப் பயன்படுத்த நீங்கள் நோக்கமிட வேண்டும்
சரியான ஸ்ட்ரீம் மேலாண்மை - அவை இனி தேவையில்லாதபோது ஸ்ட்ரீம்களை மூட நினைவில் கொள்ளுங்கள், மற்றும் ஒரே நேர ஸ்ட்ரீம்களின் எண்ணிக்கையை கண்காணிக்கவும்
சர்வர் புஷ் உத்தி - தேவைப்படக்கூடிய வளங்களை மட்டுமே தள்ளுங்கள். அதிகப்படியான தள்ளுதல் பேண்ட்விட்த் மற்றும் வளங்களை வீணாக்கும்
ஹெடர் கம்ப்ரஷன் - தனிப்பயன் ஹெடர்களின் எண்ணிக்கை மற்றும் அளவைக் குறைப்பதன் மூலம் HTTP/2 இன் ஹெடர் கம்ப்ரஷனைப் பயன்படுத்துங்கள்

HTTP/2 vs HTTP/1.1

HTTP/2 மற்றும் HTTP/1.1 க்கு இடையே உள்ள முக்கிய வேறுபாடுகள்:

அம்சம் HTTP/1.1 HTTP/2
ப்ரோட்டோகால் ஃபார்மாட் உரை-அடிப்படையிலான பைனரி-அடிப்படையிலான
மல்டிப்ளக்சிங் இல்லை (பல இணைப்புகள் தேவை) ஆம் (ஒரு இணைப்பில் பல ஸ்ட்ரீம்கள்)
ஹெடர் கம்ப்ரஷன் இல்லை ஆம் (HPACK)
சர்வர் புஷ் இல்லை ஆம்
ஃப்ளோ கன்ட்ரோல் அடிப்படை மேம்பட்ட, ஸ்ட்ரீமுக்கு ஸ்ட்ரீம்
முன்னுரிமை இல்லை ஆம்

ரியல்-வேர்ல்ட் எடுத்துக்காட்டு: முழு வலைத்தளத்தையும் சேவை செய்தல்

HTTP/2 உடன் ஒரு வலைத்தளத்தை சேவை செய்வதற்கான முழுமையான எடுத்துக்காட்டு:

const http2 = require('http2');
const fs = require('fs');
const path = require('path');
const mime = require('mime-types');

const options = {
  key: fs.readFileSync(path.join(__dirname, 'server.key')),
  cert: fs.readFileSync(path.join(__dirname, 'server.crt'))
};

const server = http2.createSecureServer(options);

// Serve files from the public directory
const publicDir = path.join(__dirname, 'public');

server.on('stream', (stream, headers) => {
  const reqPath = headers[':path'] === '/' ? '/index.html' : headers[':path'];
  const filePath = path.join(publicDir, reqPath);
  
  // Basic security check to prevent path traversal
  if (!filePath.startsWith(publicDir)) {
    stream.respond({ ':status': 403 });
    stream.end('Forbidden');
    return;
  }
  
  fs.stat(filePath, (err, stats) => {
    if (err || !stats.isFile()) {
      // File not found
      stream.respond({ ':status': 404 });
      stream.end('Not found');
      return;
    }
    
    // Determine content type
    const contentType = mime.lookup(filePath) || 'application/octet-stream';
    
    // Serve the file
    stream.respond({
      'content-type': contentType,
      ':status': 200
    });
    
    const fileStream = fs.createReadStream(filePath);
    fileStream.pipe(stream);
    
    fileStream.on('error', (err) => {
      console.error('File stream error:', err);
      stream.close(http2.constants.NGHTTP2_INTERNAL_ERROR);
    });
  });
});

server.listen(8080, () => {
  console.log('HTTP/2 server running at https://localhost:8080/');
});

📦 குறிப்பு:

இந்த எடுத்துக்காட்டிற்கு mime-types பேக்கேஜ் தேவைப்படுகிறது: npm install mime-types

மேம்பட்ட ஸ்ட்ரீம் மேலாண்மை

HTTP/2 இன் ஸ்ட்ரீம் மேலாண்மை திறன்கள் பல ஒரே நேர கோரிக்கைகளை திறமையாக கையாள அனுமதிக்கின்றன. ஸ்ட்ரீம் முன்னுரிமை மற்றும் ஃப்ளோ கன்ட்ரோலை நிரூபிக்கும் ஒரு மேம்பட்ட எடுத்துக்காட்டு இங்கே:

const http2 = require('http2');
const fs = require('fs');

// Create a server with custom settings
const server = http2.createSecureServer({
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.crt'),
  settings: {
    initialWindowSize: 65535,  // 64KB initial window
    maxConcurrentStreams: 100,
    enablePush: true
  }
});

server.on('stream', (stream, headers) => {
  // Get priority information
  const weight = stream.priority && stream.priority.weight || 1;
  const parent = stream.priority && stream.priority.parent ? 'with parent' : 'no parent';
  
  console.log(`New stream ${stream.id} (weight: ${weight}, ${parent})`);
  
  // Handle different priority levels
  if (headers[':path'] === '/high-priority') {
    stream.priority({ weight: 256, exclusive: true });
    stream.respond({ ':status': 200, 'content-type': 'text/plain' });
    stream.end('High priority content');
  } else {
    // Default priority
    stream.respond({ ':status': 200, 'content-type': 'text/plain' });
    stream.end('Standard priority content');
  }
  
  // Handle stream errors
  stream.on('error', (error) => {
    console.error(`Stream ${stream.id} error:`, error);
    stream.end();
  });
  
  // Handle stream close
  stream.on('close', () => {
    console.log(`Stream ${stream.id} closed`);
  });
});

server.listen(8443);

பிழை கையாளுதல் மற்றும் டீபக்சிங்

நம்பகமான HTTP/2 பயன்பாடுகளுக்கு சரியான பிழை கையாளுதல் முக்கியமானது. விரிவான பிழை கையாளுதலை செயல்படுத்துவது எப்படி என்பது இங்கே:

const http2 = require('http2');
const fs = require('fs');
const { promisify } = require('util');
const readFile = promisify(fs.readFile);

async function startServer() {
  try {
    const [key, cert] = await Promise.all([
      readFile('server.key'),
      readFile('server.crt')
    ]);

    const server = http2.createSecureServer({ key, cert });
    
    // Global error handler
    server.on('error', (err) => {
      console.error('Server error:', err);
      // Implement proper error recovery
    });
    
    // Handle uncaught exceptions
    process.on('uncaughtException', (err) => {
      console.error('Uncaught exception:', err);
      // Graceful shutdown
      server.close(() => process.exit(1));
    });
    
    // Handle unhandled promise rejections
    process.on('unhandledRejection', (reason, promise) => {
      console.error('Unhandled Rejection at:', promise, 'reason:', reason);
    });
    
    // Stream handler with error boundaries
    server.on('stream', (stream, headers) => {
      try {
        // Simulate async operation
        setTimeout(() => {
          try {
            if (Math.random() > 0.8) {
              throw new Error('Random error for demonstration');
            }
            stream.respond({ ':status': 200 });
            stream.end('Success!');
          } catch (err) {
            handleStreamError(stream, err);
          }
        }, 100);
      } catch (err) {
        handleStreamError(stream, err);
      }
    });
    
    function handleStreamError(stream, error) {
      console.error('Stream error:', error);
      if (!stream.destroyed) {
        stream.respond({
          ':status': 500,
          'content-type': 'text/plain'
        });
        stream.end('Internal Server Error');
      }
    }
    
    server.listen(8443, () => {
      console.log('Server running on https://localhost:8443');
    });
    
  } catch (err) {
    console.error('Failed to start server:', err);
    process.exit(1);
  }
}

startServer();

செயல்திறன் உகப்பாக்கம்

HTTP/2 செயல்திறனை உகப்பாக்குவதற்கு அதன் தனித்துவமான பண்புகளைப் புரிந்துகொள்வது தேவைப்படுகிறது. முக்கிய உத்திகள் இங்கே:

1. இணைப்பு பூலிங்

const http2 = require('http2');
const { URL } = require('url');

class HTTP2ConnectionPool {
  constructor() {
    this.connections = new Map();
  }
  
  async getConnection(url) {
    const { origin } = new URL(url);
    
    if (!this.connections.has(origin)) {
      const client = http2.connect(origin, {
        rejectUnauthorized: false // Only for development
      });
      
      // Handle connection errors
      client.on('error', (err) => {
        console.error('Connection error:', err);
        this.connections.delete(origin);
      });
      
      // Remove connection when closed
      client.on('close', () => {
        this.connections.delete(origin);
      });
      
      this.connections.set(origin, {
        client,
        lastUsed: Date.now(),
        inUse: 0
      });
    }
    
    const conn = this.connections.get(origin);
    conn.lastUsed = Date.now();
    conn.inUse++;
    
    return {
      client: conn.client,
      release: () => {
        conn.inUse--;
      }
    };
  }
  
  // Clean up idle connections
  startCleanup(interval = 30000) {
    setInterval(() => {
      const now = Date.now();
      for (const [origin, conn] of this.connections.entries()) {
        if (conn.inUse === 0 && (now - conn.lastUsed) > 60000) {
          conn.client.destroy();
          this.connections.delete(origin);
        }
      }
    }, interval);
  }
}

// Usage example
const pool = new HTTP2ConnectionPool();
pool.startCleanup();

async function makeRequest(url) {
  const { client, release } = await pool.getConnection(url);
  return new Promise((resolve, reject) => {
    const req = client.request({ ':path': new URL(url).pathname });
    let data = '';
    
    req.on('response', (headers) => {
      console.log('Status:', headers[':status']);
    });
    
    req.on('data', (chunk) => data += chunk);
    req.on('end', () => {
      release();
      resolve(data);
    });
    req.on('error', (err) => {
      release();
      reject(err);
    });
    
    req.end();
  });
}

2. ஹெடர் கம்ப்ரஷன் உகப்பாக்கம்

HTTP/2 ஹெடர்களுக்கு HPACK கம்ப்ரஷனைப் பயன்படுத்துகிறது. பின்வருவனவற்றால் உகப்பாக்கவும்:

குக்கீ அளவுகளை குறைத்தல்
குறுகிய ஆனால் விளக்கமான ஹெடர் பெயர்களைப் பயன்படுத்துதல்
நகல் ஹெடர்களைத் தவிர்த்தல்
முடிந்தால் HTTP/2-குறிப்பிட்ட ஹெடர்களைப் பயன்படுத்துதல்

பாதுகாப்பு சிறந்த நடைமுறைகள்

HTTP/2 ஐப் பயன்படுத்தும் போது, இந்த பாதுகாப்பு நடைமுறைகளைப் பின்பற்றவும்:

const http2 = require('http2');
const fs = require('fs');
const { createSecureContext } = require('tls');

// Security headers middleware
function securityHeaders(req, res, next) {
  // Set security headers
  res.setHeader('X-Content-Type-Options', 'nosniff');
  res.setHeader('X-Frame-Options', 'DENY');
  res.setHeader('X-XSS-Protection', '1; mode=block');
  res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
  res.setHeader('Content-Security-Policy', "default-src 'self'");
  
  // Remove server header
  res.removeHeader('X-Powered-By');
  
  next();
}

// Create secure server with modern TLS settings
const options = {
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.crt'),
  secureOptions:
    require('constants').SSL_OP_NO_SSLv3 |
    require('constants').SSL_OP_NO_TLSv1 |
    require('constants').SSL_OP_NO_TLSv1_1,
  ciphers: [
    'TLS_AES_256_GCM_SHA384',
    'TLS_CHACHA20_POLY1305_SHA256',
    'TLS_AES_128_GCM_SHA256'
  ].join(':'),
  minVersion: 'TLSv1.3',
  maxVersion: 'TLSv1.3',
  // OCSP Stapling
  requestCert: false,
  rejectUnauthorized: true
};

const server = http2.createSecureServer(options);

// Apply security middleware
server.on('request', (req, res) => {
  securityHeaders(req, res, () => {
    // Request handling logic
    res.setHeader('Content-Type', 'text/plain');
    res.end('Secure HTTP/2 Response');
  });
});

// Handle TLS errors
server.on('tlsClientError', (err, tlsSocket) => {
  console.error('TLS Error:', err);
  tlsSocket.destroy();
});

server.listen(8443);

ரியல்-வேர்ல்ட் பயன்பாட்டு வழக்குகள்

1. HTTP/2 உடன் API கேட்வே

HTTP/2 உடன் உயர்-செயல்திறன் API கேட்வேயைக் கட்டமைத்தல்:

const http2 = require('http2');
const { URL } = require('url');
const path = require('path');
const fs = require('fs');

// Service registry
const services = {
  '/users': 'http://users-service:3000',
  '/products': 'http://products-service:3000',
  '/orders': 'http://orders-service:3000'
};

// Create HTTP/2 server
const server = http2.createSecureServer({
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.crt')
});

// Route requests to appropriate services
server.on('stream', (stream, headers) => {
  const path = headers[':path'];
  const method = headers[':method'];
  
  try {
    // Find matching service
    const servicePath = Object.keys(services).find(prefix =>
      path.startsWith(prefix)
    );
    
    if (!servicePath) {
      stream.respond({ ':status': 404 });
      return stream.end('Not Found');
    }
    
    const targetUrl = new URL(path.slice(servicePath.length), services[servicePath]);
    
    // Forward request to target service
    const client = http2.connect(targetUrl.origin);
    const req = client.request({
      ...headers,
      ':path': targetUrl.pathname + targetUrl.search,
      ':method': method,
      ':authority': targetUrl.host
    });
    
    // Pipe the response back to client
    req.pipe(stream);
    stream.pipe(req);
    
    // Handle errors
    req.on('error', (err) => {
      console.error('Request error:', err);
      if (!stream.destroyed) {
        stream.respond({ ':status': 502 });
        stream.end('Bad Gateway');
      }
    });
    
    stream.on('error', (err) => {
      console.error('Stream error:', err);
      req.destroy();
    });
    
  } catch (err) {
    console.error('Gateway error:', err);
    if (!stream.destroyed) {
      stream.respond({ ':status': 500 });
      stream.end('Internal Server Error');
    }
  }
});

server.listen(443);

2. ரியல்-டைம் தரவு ஸ்ட்ரீமிங்

HTTP/2 உடன் திறமையான ரியல்-டைம் தரவு ஸ்ட்ரீமிங்:

const http2 = require('http2');
const fs = require('fs');

const server = http2.createSecureServer({
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.crt')
});

// In-memory storage for active streams
const streams = new Set();

// Broadcast data to all connected clients
function broadcast(data) {
  const payload = JSON.stringify(data);
  for (const stream of streams) {
    try {
      stream.write(`data: ${payload}\n\n`);
    } catch (err) {
      console.error('Stream write error:', err);
      streams.delete(stream);
    }
  }
}

// Simulate data updates
setInterval(() => {
  broadcast({
    time: new Date().toISOString(),
    value: Math.random() * 100
  });
}, 1000);

server.on('stream', (stream, headers) => {
  // Only handle GET requests
  if (headers[':method'] !== 'GET') {
    stream.respond({ ':status': 405 });
    return stream.end();
  }
  
  // Set up Server-Sent Events headers
  stream.respond({
    'content-type': 'text/event-stream',
    'cache-control': 'no-cache',
    'connection': 'keep-alive',
    ':status': 200
  });
  
  // Add to active streams
  streams.add(stream);
  
  // Handle client disconnect
  stream.on('close', () => {
    streams.delete(stream);
  });
  
  // Send initial data
  stream.write('event: connect\ndata: Connected\n\n');
});

server.listen(8443, () => {
  console.log('HTTP/2 Server running on https://localhost:8443');
});

பயிற்சி

சரியான தொகுப்பு பெயரை தேர்வு செய்யவும்.

The ______ module provides an implementation of the HTTP/2 protocol in Node.js.

http
✗ தவறு! "http" தொகுதி HTTP/1.1 ப்ரோட்டோகாலை வழங்குகிறது
https
✗ தவறு! "https" தொகுதி HTTP/1.1 ப்ரோட்டோகாலுக்கான TLS/SSL ஆதரவை வழங்குகிறது
http2
✓ சரி! "http2" தொகுதி Node.js இல் HTTP/2 ப்ரோட்டோகாலின் செயல்படுத்தலை வழங்குகிறது
net
✗ தவறு! "net" தொகுதி அடிப்படை நெட்வொர்க்கிங் செயல்பாடுகளை வழங்குகிறது